﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Profile;
using AmkeniCrossLoginApplication.Data_Access;
using AmkeniCrossLoginApplication.Classes;

namespace AmkeniCrossLoginApplication
{
    public partial class MyProfile : Security
    {

        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            string name = User.Identity.Name;

            try
            {
                var loggedInUser = (from data in db.users
                                    where
                                        data.email == name
                                    select data).FirstOrDefault();

                //FirstNameTextBox.Text = loggedInUser.first_name;
                //LastNameTextBox.Text = loggedInUser.last_name;
                //EmailTextBox.Text = loggedInUser.user_email;

                FirstNameTextBox.Attributes.Add("value", loggedInUser.first_name);
                LastNameTextBox.Attributes.Add("value", loggedInUser.last_name);

                //PasswordTextBox1.Text = base64Decode(loggedInUser.password);
                //PasswordTextBox2.Text = base64Decode(loggedInUser.password);
                PasswordTextBox1.Attributes.Add("value", base64Decode(loggedInUser.password));
                PasswordTextBox2.Attributes.Add("value", base64Decode(loggedInUser.password));
            }
            catch (Exception ex)
            {
                throw new Exception("PROFILE ERROR: " + ex.Message + " " + ex.StackTrace);
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            string name = User.Identity.Name;

            try
            {
                user loggedInUser = db.users.SingleOrDefault(
                                        u => u.email == name);

                if (loggedInUser != null)
                {
                    loggedInUser.first_name = FirstNameTextBox.Text.Trim();
                    loggedInUser.last_name = LastNameTextBox.Text.Trim();

                    loggedInUser.password = base64Encode(PasswordTextBox1.Text.Trim());

                    db.SaveChanges();
                    Response.Redirect("~/MyProfile.aspx");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("PROFILE ERROR: " + ex.Message + " " + ex.StackTrace);
            }
        }

    }
}